/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package estates;

import java.util.*;
import estates.facade.*;
import estates.model.*;
import com.sun.rave.web.ui.appbase.AbstractApplicationBean;
import javax.faces.FacesException;
import com.sun.webui.jsf.model.Option;
import com.sun.webui.jsf.model.OptionsList;
import estates.util.TransferEstate;
import estates.opcje.StaticOptionList;
import estates.util.ErrorCodes;
import estates.util.TransferComment;
import estates.util.TransferUser;
import estates.validation.StringLengthValidator;

/**
 * <p>Application scope data bean for your application.  Create properties
 *  here to represent cached data that should be made available to all users
 *  and pages in the application.</p>
 *
 * <p>An instance of this class will be created for you automatically,
 * the first time your application evaluates a value binding expression
 * or method binding expression that references a managed bean using
 * this class.</p>
 *
 * @version ApplicationBean1.java
 * @version Created on 2011-03-31, 14:53:07
 * @author Przemek
 */

public class ApplicationBean1 extends AbstractApplicationBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    // </editor-fold>

    /**
     * <p>Construct a new application data bean instance.</p>
     */

     /*   private ArrayList<Option> zabudowa = new ArrayList();
private ArrayList<Option> typuzytkownika = new ArrayList();
        private ArrayList<Option> typlokalu = new ArrayList();
        private ArrayList<Option> typoferty = new ArrayList();
        private ArrayList<Option> stanbudynku = new ArrayList();*/

      private StringLengthValidator stringLengthValidator = new StringLengthValidator(150);

    public StringLengthValidator getStringLengthValidator() {
        return stringLengthValidator;
    }

    public void setStringLengthValidator(StringLengthValidator stringLengthValidator) {
        this.stringLengthValidator = stringLengthValidator;
    }



    private StaticOptionList optRozkladowe =
            new StaticOptionList(0, new String[]{"Tak", "Nie"});

    private StaticOptionList optStan =
            new StaticOptionList(0, new String[]{"Nie wybrano","Wieczyste użytkowanie","Własność","Wspólnota"});

    private StaticOptionList optUserType =
            new StaticOptionList(0, new String[]{"Zwykły", "Admin", "Pracownik", "Agent", "Firma"});

    private StaticOptionList optTypOferty =
            new StaticOptionList(1, new String[]{"Budynek", "Lokal"});

    private StaticOptionList optTypLokalu =
            new StaticOptionList(0, new String[]{"Typ 0", "Typ 1", "Typ 2"});

    private StaticOptionList optStanBudynku =
            new StaticOptionList(0, new String[]{"Stan 0", "Stan 1", "Stan 2"});

    private StaticOptionList optTypZabudowy =
            new StaticOptionList(0, new String[]{"Nie wybrano", "Punktowiec", "Klatkowiec", "Korytarzowiec",
                                                 "Galeriowiec", "Szeregowa", "Bliźniacza", "Wolnostojąca"});

    private StaticOptionList optOceny =
            new StaticOptionList(1, new String[]{"1","2","3","4","5","6","7","8","9","10"});

    public StaticOptionList getOptOceny() {
        return optOceny;
    }

    public void setOptOceny(StaticOptionList optOceny) {
        this.optOceny = optOceny;
    }



    public StaticOptionList getOptRozkladowe() {
        return optRozkladowe;
    }

    public void setOptRozkladowe(StaticOptionList optRozkladowe) {
        this.optRozkladowe = optRozkladowe;
    }

    public StaticOptionList getOptStan() {
        return optStan;
    }

    public void setOptStan(StaticOptionList optStan) {
        this.optStan = optStan;
    }

    public StaticOptionList getOptStanBudynku() {
        return optStanBudynku;
    }

    public void setOptStanBudynku(StaticOptionList optStanBudynku) {
        this.optStanBudynku = optStanBudynku;
    }

    public StaticOptionList getOptTypLokalu() {
        return optTypLokalu;
    }

    public void setOptTypLokalu(StaticOptionList optTypLokalu) {
        this.optTypLokalu = optTypLokalu;
    }

    public StaticOptionList getOptTypOferty() {
        return optTypOferty;
    }

    public void setOptTypOferty(StaticOptionList optTypOferty) {
        this.optTypOferty = optTypOferty;
    }

    public StaticOptionList getOptTypZabudowy() {
        return optTypZabudowy;
    }

    public void setOptTypZabudowy(StaticOptionList optTypZabudowy) {
        this.optTypZabudowy = optTypZabudowy;
    }

    public StaticOptionList getOptUserType() {
        return optUserType;
    }

    public void setOptUserType(StaticOptionList optUserType) {
        this.optUserType = optUserType;
    }

    public OptionsList getCommentOptionsList() {
        List<Comment> comments = getFasada().getKomentarze();
        Option []tab = new Option[comments.size()];
        int i = 0;
        for(Comment u : comments){
            //todo
            tab[i++] = new Option(Long.toString(u.getId()), u.getTitle());
        }
        OptionsList opt = new OptionsList();
        opt.setOptions(tab);
        return opt;
    }

    public OptionsList getUserOptionsList() {
        List<User> users = getFasada().getUzytkownicy();
        Option []tab = new Option[users.size()];
        int i = 0;
        for(User u : users){
            //todo
            tab[i++] = new Option(u.getNIP(), u.getLogin());
        }
        OptionsList opt = new OptionsList();
        opt.setOptions(tab);
        return opt;
    }

    public OptionsList getEstateOptionsList() {
        List<Estate> estates = getFasada().getOferty();
        Option []tab = new Option[estates.size()];
        int i = 0;
        for(Estate u : estates){
            TransferEstate t = new TransferEstate(u.toString());
            String desc = "ID: " + t.getNumer_oferty();
            tab[i++] = new Option(t.getNumer_oferty(), desc);
        }
        OptionsList opt = new OptionsList();
        opt.setOptions(tab);
        return opt;
    }

    private Fasada1 fasada;
    private Fasada2 fasadaHibernate;

    public Fasada2 getFasadaHibernate() {
      return fasadaHibernate;
    }

    public void setFasadaHibernate(Fasada2 fasadaHibernate) {
      this.fasadaHibernate = fasadaHibernate;
    }

    public Fasada1 getFasada() {
        return fasada;
    }

    public void setFasada(Fasada1 fasada) {
        this.fasada = fasada;
    }

    @Deprecated
    public ArrayList<Option> getOferty(){
        ArrayList<Option> a = new ArrayList<Option>();

       Iterator<Estate> it = fasada.getOferty().iterator();

        int i = 0;

        while(it.hasNext()){
            a.add(new Option(i++, it.next().toString()));
        }

        //a.add(new Option(1, "a"));
        //a.add(new Option(2, "b"));

        return a;
    }

    @Deprecated
    public List<Estate> getOfertyList(){
        ArrayList<Estate> a = new ArrayList();

       Iterator it = fasada.getOferty().iterator();

        int i = 0;

        while(it.hasNext()){
            a.add((Estate)it.next());
        }

        //a.add(new Option(1, "a"));
        //a.add(new Option(2, "b"));

        return a;
    }

    @Deprecated
    private List<TransferEstate> getAndParse(ArrayList<Estate> es){
        ArrayList<TransferEstate> a = new ArrayList();

        Iterator it = es.iterator();

        int i = 0;

        while(it.hasNext()){
            TransferEstate s = new TransferEstate(((Estate)it.next()).toString());
            a.add(s);
        }

        return a;
    }

    public List<TransferEstate> getOfertyStringList(){
        //return getAndParse(fasada.getOferty());
        ArrayList<TransferEstate> a = new ArrayList();

        Iterator it = fasada.getOferty().iterator();

        int i = 0;

        while(it.hasNext()){
            TransferEstate s = new TransferEstate(((Estate)it.next()).toString());
            a.add(s);
        }

        return a;
    }

    public List<TransferEstate> getOfertyWyszukaj(HashMap<String,Object> wz){
        ArrayList<TransferEstate> a = new ArrayList();
        Iterator it = fasada.wyszukaj(wz).iterator();
        int i = 0;
        while(it.hasNext()){
            TransferEstate s = new TransferEstate(((Estate)it.next()).toString());
            a.add(s);
        }
        return a;
    }

    public List<TransferEstate> getOfertyPorownaj(HashMap<String,Object> wz){
        ArrayList<TransferEstate> a = new ArrayList();
        Iterator it = fasada.zestawOferty(wz).iterator();
        int i = 0;
        while(it.hasNext()){
            TransferEstate s = new TransferEstate(((Estate)it.next()).toString());
            a.add(s);
        }
        return a;
    }

    public List<TransferEstate> getOfertyUseraStringList(String u){
        //return getAndParse(fasada.getOferty());
        ArrayList<TransferEstate> a = new ArrayList();

        Iterator it = fasada.getOfertyUzytkownika(u).iterator();

        int i = 0;

        while(it.hasNext()){
            TransferEstate s = new TransferEstate(((Estate)it.next()).toString());
            a.add(s);
        }

        return a;
    }

    public List<TransferEstate> getObserwowaneUseraStringList(String u){
        //return getAndParse(fasada.getOferty());
        ArrayList<TransferEstate> a = new ArrayList();

        Iterator<Estate> it = fasada.getObserwowaneUzytkownika(u).iterator();

        int i = 0;

        while(it.hasNext()){
            TransferEstate s = new TransferEstate((it.next()).toString());
            a.add(s);
        }

        return a;
    }

    public List<TransferEstate> getOfertyLokalStringList(){
        //return getAndParse(fasada.getOferty());
        ArrayList<TransferEstate> a = new ArrayList();

        Iterator it = fasada.getOferty().iterator();

        int i = 0;

        while(it.hasNext()){
            Estate e = (Estate)it.next();
            if(e instanceof DekoratorLokalowy){
                TransferEstate s = new TransferEstate(e.toString());
                a.add(s);
            }
        }

        return a;
    }

    public List<TransferEstate> getOfertyBudynekStringList(){
        //return getAndParse(fasada.getOferty());
        ArrayList<TransferEstate> a = new ArrayList();

        Iterator it = fasada.getOferty().iterator();

        int i = 0;

        while(it.hasNext()){
            Estate e = (Estate)it.next();
            if(e instanceof DekoratorBudynkowy){
                TransferEstate s = new TransferEstate(e.toString());
                a.add(s);
            }
        }

        return a;
    }

    public List<TransferComment> getUserCommentsStringList(String id){
        //return getAndParse(fasada.getUzytkownicy());
        ArrayList<TransferComment> a = new ArrayList();

        Iterator it = fasada.getUsersComments(fasada.Szukaj_user(id)).iterator();

        int i = 0;

        while(it.hasNext()){
            TransferComment s = new TransferComment(((Comment)it.next()).toString());
            a.add(s);
        }

        return a;
    }

    public List<TransferComment> getEstateCommentsStringList(String id){
        //return getAndParse(fasada.getUzytkownicy());
        ArrayList<TransferComment> a = new ArrayList();

        Iterator it = fasada.getEstatesComments(id).iterator();

        int i = 0;

        while(it.hasNext()){
            TransferComment s = new TransferComment(((Comment)it.next()).toString());
            a.add(s);
        }

        return a;
    }

    public List<TransferUser> getUserStringList(){
        //return getAndParse(fasada.getUzytkownicy());
        ArrayList<TransferUser> a = new ArrayList();

        Iterator it = fasada.getUzytkownicy().iterator();

        int i = 0;

        while(it.hasNext()){
            TransferUser s = new TransferUser(((User)it.next()).toString());
            a.add(s);
        }

        return a;
    }

    public List<User> getUserList(){
        ArrayList<User> a = new ArrayList();

       Iterator it = fasada.getUzytkownicy().iterator();

        int i = 0;

        while(it.hasNext()){
            a.add((User)it.next());
        }

        //a.add(new Option(1, "a"));
        //a.add(new Option(2, "b"));

        return a;
    }

    public List<Comment> getUserComments(String uid){
        return fasada.getUsersComments(fasada.Szukaj_user(uid));
    }
    
    int dodajOferte(HashMap<String,Object> a1,HashMap<String,Object> a2){
        Estate es = this.getFasada().Dodajoferte(a1, a2);
        if(es != null){
            getFasadaHibernate().persistEstate(es);
            getFasadaHibernate().updateUser(es.getUser());
            return es.getNumer_oferty();
        }else{
            return ErrorCodes.EST_ERROR;
        }
    }

    int dodajKomentarz(HashMap<String,Object> hm){
        Comment c = this.getFasada().Dodajkomentarz(hm);
        if(c != null){
            getFasadaHibernate().persistComment(c);
            getFasadaHibernate().updateEstate(c.getEstate());
            return ErrorCodes.COM_OK;
        }else{
            return ErrorCodes.COM_ERROR;
        }
    }


    public ApplicationBean1() {
    }

    /**
     * <p>This method is called when this bean is initially added to
     * application scope.  Typically, this occurs as a result of evaluating
     * a value binding or method binding expression, which utilizes the
     * managed bean facility to instantiate this bean and store it into
     * application scope.</p>
     * 
     * <p>You may customize this method to initialize and cache application wide
     * data values (such as the lists of valid options for dropdown list
     * components), or to allocate resources that are required for the
     * lifetime of the application.</p>
     */
    @Override
    public void init() {

       
        // Perform initializations inherited from our superclass
        super.init();

        fasadaHibernate = new Fasada2();
        fasada = new Fasada1(fasadaHibernate.listComments(), fasadaHibernate.listEstates(), fasadaHibernate.listUsers());

        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("ApplicationBean1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here

        
    }

    /**
     * <p>This method is called when this bean is removed from
     * application scope.  Typically, this occurs as a result of
     * the application being shut down by its owning container.</p>
     * 
     * <p>You may customize this method to clean up resources allocated
     * during the execution of the <code>init()</code> method, or
     * at any later time during the lifetime of the application.</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return an appropriate character encoding based on the
     * <code>Locale</code> defined for the current JavaServer Faces
     * view.  If no more suitable encoding can be found, return
     * "UTF-8" as a general purpose default.</p>
     *
     * <p>The default implementation uses the implementation from
     * our superclass, <code>AbstractApplicationBean</code>.</p>
     */
    @Override
    public String getLocaleCharacterEncoding() {
        return super.getLocaleCharacterEncoding();
    }

}
